package linlist;
//Einfach verkettete Lineare Liste mit Hilfe von Knoten
///linearlist///
public class LinearList1{
  protected int anz=0;
  protected Knoten head=new Knoten(null);

  protected Knoten getNode(int i){
    Knoten wander=head;
    while(wander.next!=null && i-->=0)
      wander=wander.next;
    return wander;
  }

  public boolean isempty(){return anz==0;}

  public int length(){return anz;}

  public Object get(int wo){
    if(wo<0 || wo>=anz) return null;
    else return getNode(wo).data;
  }

  public void insert(Object obj, int wo){
    Knoten neu=new Knoten(obj);
    Knoten vor=getNode(wo-1);
    neu.next=vor.next;
    vor.next=neu;
    anz++;
  }

  public Object delete(int wo){
    if(wo<0 || wo>=anz) return null;
    Knoten vor=getNode(wo-1);
    Knoten akt=vor.next;
    vor.next=akt.next;
    anz--;
    return akt.data;
  }

  public int indexOf(Object obj, int start){
    if(start>=0 && start<anz){
      Knoten wander=getNode(start-1);
      while(wander.next!=null){
        wander=wander.next;
        if(wander.data==obj) return start;
        start++;
      }
    }
    return -1;
  }

  public boolean equals(Object obj){
    if(!(obj instanceof LinearList1)) return false;
    LinearList1 ll=(LinearList1)obj;
    if(anz!=ll.anz) return false;
    Knoten a=head.next;
    Knoten b=ll.head.next;
    while(a!=null){
      if(a.data!=b.data) return false;
      a=a.next;
      b=b.next;
    }
    return true;
  }

  public String toString(){
    Knoten wander=head.next;
    StringBuffer sb=new StringBuffer();
    while(wander!=null){
      sb.append(wander.data.toString());
      sb.append(" ");
      wander=wander.next;
    }
    return sb.toString();
  }
///linearlistend///
///node///
  public static class Knoten{
    public Object data;
    public Knoten next;
    
    public Knoten(Object d){
      data=d;
    }
  }
///nodeend///
}
